package com.kbt.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.*;
import com.google.gson.Gson;
import com.kbt.activity.AddressMangeActivity;
import com.kbt.activity.GoodForPayActivity;
import com.kbt.activity.R;
import com.kbt.adapter.OrderBaseAdapter;
import com.kbt.commont.Constants;
import com.kbt.listener.PopWindowDismissListener;
import com.kbt.model.data.BaseBean;
import com.kbt.model.data.OrderBean;
import com.kbt.model.data.OrderInfoModel;
import com.kbt.net.RequestManager;
import com.kbt.util.tool.BtnSetting;
import com.kbt.util.tool.PopWindowSetting;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhgk
 * @package: com.kbt.ui
 * @TODO: <请描述这个类是干什么的>
 * @data: 2015/8/20 15:31
 * @version: V1.0
 */
public class CancelOrderPopWindow extends PopupWindow implements RequestManager.ResponseInterface{

    private Activity context;
    private EditText cancelReason;
    private View vi;
    private View positionView;
    private RequestManager mRequestManager;
    private Map<String, String> map =null;
    private int posi;
    private List<OrderBean> orderList;
    private BaseBean baseBean;
    private OrderBaseAdapter orderAdapter;

    public CancelOrderPopWindow(Activity context, List<OrderBean> orderList, OrderBaseAdapter orderAdapter, int position, View positionView) {
        //初始化信息
        super(context);
        this.context = context;
        this.positionView = positionView;
        this.posi = position;
        this.orderList = orderList;
        this.orderAdapter = orderAdapter;
        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, String>();


        //获取布局信息
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vi = inflater.inflate(R.layout.cancle_order_dialog, null);

        TextView title = (TextView) vi.findViewById(R.id.dialog_title);
        //设置标题
        title.setText("取消订单：" + orderList.get(position).getOrder_no());

        cancelReason = (EditText) vi.findViewById(R.id.cancelReson);

        Button okBtn = (Button) vi.findViewById(R.id.okBtn);

        Button cancelBtn = (Button) vi.findViewById(R.id.canBtn);

//        BtnSetting.setBtnBackground(okBtn, 2, Color.rgb(221, 25, 29), Color.rgb(232, 78, 64), 5);
        okBtn.setTextColor(Color.WHITE);

//        BtnSetting.setBtnBackground(cancelBtn, 2, Color.rgb(84, 110, 122), Color.rgb(120, 144, 156), 5);
        cancelBtn.setTextColor(Color.WHITE);

        this.setContentView(vi);
        //设置弹出窗口样式
        PopWindowSetting.setPopWindowStyle(this);

        //设置父窗体背景
        PopWindowSetting.setParentBackGround(context, 0.3f);

        //设置窗口销毁时事件
        PopWindowDismissListener dismissListener = new PopWindowDismissListener(context);
        this.setOnDismissListener(dismissListener);


        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String reason = cancelReason.getText().toString().trim();
//                if (reason.length() == 0) {
//                    Toast.makeText(context, "请填写取消原因，谢谢！", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                //取消订单
                httpCancelOrder();

            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }
    //取消订单
    private void httpCancelOrder(){
        map.clear();
        map.put("methodType", "cancel");
        Gson gson = new Gson();
        Map dataMap = new HashMap<String,String>();
        dataMap.put("order_no",orderList.get(posi).getOrder_no());
        dataMap.put("cancel_reason", cancelReason.getText().toString());
        dataMap.put("cancel_time",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        map.put("data", gson.toJson(dataMap));
        mRequestManager.post(Constants.ServiceInterFace.canCelOrderURL, map, Constants.RequestTag.canCelOrderTag, BaseBean.class);
    }


    @Override
    public <T> void successResponse(T parsedGSON, int tag) {
        if (tag == Constants.RequestTag.canCelOrderTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                baseBean = (BaseBean) parsedGSON;
                if(baseBean.getResult().equals("success")){
                    Toast.makeText(context,"取消成功",Toast.LENGTH_SHORT).show();
                    //将原因传递至服务器
                    orderList.remove(posi);
                    orderAdapter.notifyDataSetChanged();
                    dismiss();
                }else{
                    Toast.makeText(context,""+baseBean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    @Override
    public void errorResonse(String retmeg, int tag) {
        Toast.makeText(context,"您的网络不给力",Toast.LENGTH_SHORT).show();
    }
}
